package com.beyond.trace.service.impl.warn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beyond.trace.config.IotConfig;
import com.beyond.trace.domain.warn.Device;
import com.beyond.trace.service.warn.DeviceService;
import com.ruoyi.common.config.RuoYiConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: DeviceServiceImpl
 * @author: z越
 * @date: 2023/2/28  14:56
 * @description 设备接口实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private IotConfig iotConfig;


    @Override
    public List<Device> findAllByDeviceType(String deviceType) {

        List<Device> deviceList = new ArrayList<>();
        String iotDevice = findIotDevice();
        if(StringUtils.isNotEmpty(iotDevice)) {
            try {
                JSONObject jsonObject = JSON.parseObject(iotDevice);
                if("200".equals(jsonObject.getString("code"))) {
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    if(com.ruoyi.common.utils.StringUtils.isNotEmpty(jsonArray)) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject data = jsonArray.getJSONObject(i);
                            Device device = new Device();
                            device.setType(data.getString("productId"));
                            device.setId(data.getLong("id"));
                            device.setAlias(data.getString("deviceName"));
                            device.setStatus(data.getString("state"));
                            device.setDevice_no(data.getString("deviceId"));
                            deviceList.add(device);
                        }
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return deviceList;
    }

    private String findIotDevice(){
        OkHttpClient client = new OkHttpClient();
        String url = iotConfig.getUrl() + "/becloud-rule/screen/devices?username="+iotConfig.getUsername()+
                "&password="+iotConfig.getPassword();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.code() != 200) {
                log.info("请求绿色云农获取设备信息接口失败:{}，{}", response.message(),
                        url);
            } else {
                String str = Objects.requireNonNull(response.body()).string();
                log.info(str);
                return str;
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }
}
